# class Solution:
#     def minKBitFlips(self, A: List[int], K: int) -> int:
#         left=0
#         count=0
#         while left<=len(A)-K:
#             if A[left]==0:
#                 for i in range(left,left+K):
#                     A[i]=not A[i]
#                 count+=1
#             left+=1
#         while left<len(A):
#             if A[left]==0:
#                 return -1
#             left+=1
#         return count
'''
超时
'''

# class Solution:
#     def minKBitFlips(self, A: List[int], K: int) -> int:
#         number=sum([2**index for index,i in enumerate(A) if i])
#         tester=sum([2**i for i in range(K)])
#         go=1
#         big=2**len(A)
#         end=2**(len(A)-K+1)
#         count=0
#         while go<end:
#             if not number&go:
#                 number=number ^ tester
#                 count+=1
#             go<<=1
#             tester<<=1
#         if number!=big-1:
#             return -1
#         return count

'''
超时
'''

class Solution:
    def minKBitFlips(self, A: List[int], K: int) -> int:
        diff=[0]*(len(A)+1)
        ans=0
        c=0
        for i in range(len(A)):
            c+=diff[i]
            if (A[i]+c)%2==0:
                if i+K>len(A):
                    return -1
                ans+=1
                c+=1
                diff[i+K]-=1
        return ans

'''
执行用时：
880 ms
, 在所有 Python3 提交中击败了
68.57%
的用户
内存消耗：
15.6 MB
, 在所有 Python3 提交中击败了
63.81%
的用户
'''